What is the difference between readonly, const, and
static?
What is the difference between readonly, const, and static?
240
19-Jun-2025
Ponu Maurya
24-Jun-2025conststatic.Use when: Value is truly constant and known at compile time (like Pi, conversion factors, etc.)
readonlyUse when: Value should be set once at runtime (e.g., ID, creation timestamp).
staticUse when: You want a member that is shared across all instances (like configuration, counters, etc.)
Combined Usage
You can combine
staticwithreadonly:Means: shared across all instances, value set at runtime, but not modifiable afterward.
Summary Table
constreadonlystaticreadonly)